package com.udhtu.ii.lab2;

import static com.udhtu.ii.lab2.Function.*;


public class FirstMethod {
    private double x;
    private double y;
    private double step;
    private double Xnew, Ynew, InewX, InewY;

    public FirstMethod() {
        this.x = X_FIRST;
        this.y = Y_FIRST;
        this.step = STEP;
        Xnew = 0.0;
        Ynew = 0.0;
        InewX = 0.0;
        InewY = 0.0;
    }

    public void firstMethod() {


        double i = 1, fun, FX, FY;
        fun = function(x, y);

        FX = fun;
        FY = fun;

        while (i < N) {
            x = x + step;
            fun = function(x, y);
            if (fun < FX) {
                FX = fun;
                Xnew = x;
                InewX = i;
            } else {
                x = x - step - step;
                fun = function(x, y);
                if (fun < FX) {
                    FX = fun;
                    Xnew = x;
                    InewX = i;
                }
            }


            y = y + step;
            fun = function(x, y);
            if (fun < FY) {
                FY = fun;
                Ynew = y;
                InewY = i;
            } else {
                y = y - step - step;
                fun = function(x, y);
                if (fun < FY) {
                    FY = fun;
                    Ynew = y;
                    InewY = i;
                }
            }
            i++;
        }
    }

    public void compute() {
        this.firstMethod();
        System.out.println("Минимум функции в точке : [" + Xnew + ";" + Ynew + "]");
        System.out.println("Значение функции: " + function(Xnew, Ynew));
        System.out.println("Количество итераций по X = " + InewX);
        System.out.println("Количество итераций по Y = " + InewY);
    }
    }
